package interactiond

import (
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
)

const (
	// DomainID 领域唯一标志
	DomainID = "interaction"
)

// DomainOption 域构建参数
type DomainOption struct {
	GroupRepo       GroupRepo
	TypeRepo        TypeRepo
	ReplayRepo      ReplayRepo
	InteractionRepo InteractionRepo
}

// NewDomain 核心子域内容信息域
func NewDomain(p ddd.Domain, opt DomainOption) InteractionDomain {
	d := &interactionDomain{}
	d.Init(d, p, DomainID)
	initGroup(d, opt.GroupRepo)
	initType(d, opt.TypeRepo)
	initReplay(d, opt.ReplayRepo)
	initInteraction(d, opt.InteractionRepo)
	return d
}

// InteractionDomain 信息域
type InteractionDomain interface {
	ddd.Domain
	GroupAggregateRoot() GroupAggregateRoot
	TypeAggregateRoot() TypeAggregateRoot
	ReplayAggregateRoot() ReplayAggregateRoot
	InteractionAggregateRoot() InteractionAggregateRoot
}
type interactionDomain struct {
	ddd.BaseDomain
	groupAggregateRoot       GroupAggregateRoot
	typeAggregateRoot        TypeAggregateRoot
	replayAggregateRoot      ReplayAggregateRoot
	interactionAggregateRoot InteractionAggregateRoot
}

func (i *interactionDomain) GroupAggregateRoot() GroupAggregateRoot {
	if i.groupAggregateRoot != nil {
		return i.groupAggregateRoot
	}
	r := i.BaseDomain.AggregateRootByID(Group)
	if r == nil {
		panic("没有初始化GroupAggregateRoot")
	}
	ir, ok := r.(GroupAggregateRoot)
	if !ok {
		panic("初始化GroupAggregateRoot，类型错误")
	}
	i.groupAggregateRoot = ir
	return ir

}

func (i *interactionDomain) TypeAggregateRoot() TypeAggregateRoot {
	if i.typeAggregateRoot != nil {
		return i.typeAggregateRoot
	}
	r := i.BaseDomain.AggregateRootByID("type")
	if r == nil {
		panic("没有初始化TypeAggregateRoot")
	}
	ir, ok := r.(TypeAggregateRoot)
	if !ok {
		panic("初始化TypeAggregateRoot，类型错误")
	}
	i.typeAggregateRoot = ir
	return ir

}
func (i *interactionDomain) ReplayAggregateRoot() ReplayAggregateRoot {
	if i.replayAggregateRoot != nil {
		return i.replayAggregateRoot
	}
	r := i.BaseDomain.AggregateRootByID(Replay)
	if r == nil {
		panic("没有初始化ReplayAggregateRoot")
	}
	ir, ok := r.(ReplayAggregateRoot)
	if !ok {
		panic("初始化ReplayAggregateRoot，类型错误")
	}
	i.replayAggregateRoot = ir
	return ir

}
func (i *interactionDomain) InteractionAggregateRoot() InteractionAggregateRoot {
	if i.interactionAggregateRoot != nil {
		return i.interactionAggregateRoot
	}
	r := i.BaseDomain.AggregateRootByID("interaction")
	if r == nil {
		panic("没有初始化InteractionAggregateRoot")
	}
	ir, ok := r.(InteractionAggregateRoot)
	if !ok {
		panic("初始化InteractionAggregateRoot，类型错误")
	}
	i.interactionAggregateRoot = ir
	return ir

}
